<?php
class Forgetpass_Controller extends Template_Controller {

	public $template = 'client/index';
	
    public function __construct()
    {
        parent::__construct();
		 //Css
        $this->template->css = $this->site['theme_url'].'login/login.css';
        //View
        $this->get_view();
    }
    
    public function index()
    {	
        $this->template->content = new View('login/frm_forgetpass');
		if($this->session->get('error_msg'))
		{
			$this->template->content->error_msg = $this->session->get('error_msg');
		}				
    }
    
	private function _valid_forgotPass()
	{
		$email = $this->input->post('txt_email');	
		$result = ORM::factory('customer')->where('customers_email',$email)->find();
		if($result->count_all())
			return $result->customers_id;
		else		
			return false;	
	}
	
	private function send_email($result)
    {
    	//Use connect() method to load Swiftmailer
		$swift = email::connect();
		 
		//From, subject
		//$from = array($this->site['site_email'], 'Yesnotebook get password');
		
		$from = $this->site['site_email'];
		$subject = 'Forgot your password '.$this->site['site_name'];
		//HTML message
		$path = 'application/views/email_tpl/forgotpass.tpl';
		$fi = fopen($path,'r+');
		$html_content = file_get_contents($path); 	
		//Replate content	
		$name = $result->customers_firstname.' '.$result->customers_lastname;		
		$html_content = str_replace('#first_name#',$name,$html_content);		
		$html_content = str_replace('#username#',$result->customers_username,$html_content);		
		$html_content = str_replace('#sitename#','YesNoteBook',$html_content);		
		$html_content = str_replace('#password#',$result->customers_pass,$html_content);
		//fwrite($fi, $html_content);
		fclose($fi);

		//Build recipient lists
		$recipients = new Swift_RecipientList;
		$recipients->addTo($result->customers_email);
		 
		 //Build the HTML message
		$message = new Swift_Message($subject, $html_content, "text/html");
		 
		if($swift->send($message, $recipients, $from)){
			url::redirect('forgetpass/success');
			die();
		} else {
			
		}		 
		// Disconnect
		$swift->disconnect();
    }
    
	private function _get_record()
    {
    	$form = array
	    (
	       'txt_email' => ''	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_email','required','email');
			if($post->validate())
 			{
 				$form = arr::overwrite($form, $post->as_array());
 				return $form; 				
			} else {
				$errors = arr::overwrite($errors, $post->errors('forgetpass_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				url::redirect('forgetpass');
				die();
			}
        }
    }
	
	public function sm()
    {
		$record = $this->_get_record();
		$valid = $this->_valid_forgotPass();
		if ($valid)	// if login access
		{
			$result = ORM::factory('customer',$valid);
			$this->send_email($result);
			
			$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_login_success'));
			
			//url::redirect('login');
			die();			
		} else {
			$this->session->set_flash('error_msg',Kohana::lang('forgetpass_lang.lbl_email_exit'));
			url::redirect('forgetpass');
			die();
		}
	}
	
	public function success()
    {	
        $this->template->content = new View('login/success');
		$this->template->left = new View('client/left');						
    }
}
?>